package com.system.servlet;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.model.Student;
import com.system.service.StudentService;
import com.system.util.ReflectUtil;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class studentServlet
 */
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("utf-8");
		response.setHeader("content-type", "text/html; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*"); 
		ReflectUtil.invokeMethods(this.getClass(), request, response);
	}
	/**
	 * 查询所有学生的信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
		StudentService service=new StudentService();
		JSONObject json=service.queryAll();
		response.getWriter().write(json.toString());
		System.out.println(json.toString());
	}
	/**
	 * 根据班级查询学生信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryByClass(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String clazz= request.getParameter("stuclass");
		StudentService service=new StudentService();
		JSONObject json=service.checkoutQueryStusbyClass(clazz);
		response.getWriter().write(json.toString());
	}
	/**
	 * 根据学号查询所在班级学生信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryByNum(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String stunum=request.getParameter("stunum");
		StudentService service=new StudentService();
		JSONObject json=service.checkoutQueryStusbyStunum(stunum);
		response.getWriter().write(json.toString());
	}
	
	
	/**
	 * 学生查询信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryStudentByUserid(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String str=request.getParameter("user_id");
		//todo solve exception
		int user_id = Integer.parseInt(str);
		StudentService service=new StudentService();
		JSONObject json=service.queryInfo(user_id);
		response.getWriter().write(json.toString());
		System.out.println(json.toString());
	}
	/**
	 * 业务层，根据学号查询学生个人信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void querybyStunum(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String stunum=request.getParameter("stunum");
		StudentService service=new StudentService();
		JSONObject json=service.checkoutqueryInfo(stunum);
		response.getWriter().write(json.toString());
	}
	/**
	 * 添加学生信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void addStudent(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String stuname = request.getParameter("stuname");
		String stuclass = request.getParameter("stuclass");
		String major = request.getParameter("major");
		String stunum = request.getParameter("stunum");
		
		//【注】user_id是关联user表的外键，依赖于user表的自动生成，添加操作不用在前端参数中拿
//		String str=request.getParameter("user_id");
		//todo solve exception
//		int user_id = Integer.parseInt(str);
		Student stu = new Student();
		stu.setMajor(major);
		stu.setStuclass(stuclass);
		stu.setStunum(stunum);
		stu.setStuname(stuname);
//		stu.setUser_id(user_id);
		stu.setUser_id(0);
		
		StudentService service=new StudentService();
		JSONObject json=service.add(stu);
		response.getWriter().write(json.toString());
	}
	/**
	 * 增加反馈信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void addRemark (HttpServletRequest request, HttpServletResponse response) throws Exception{
		String teanum=request.getParameter("teanum");
		String stunum=request.getParameter("stunum");
		String remark=request.getParameter("remark");
		StudentService service=new StudentService();
		JSONObject json=service.checkoutAddRemarks(stunum, teanum, remark);
		response.getWriter().write(json.toString());
		
	}
	/**
	 * 修改学生信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String[] ids = request.getParameterValues("userIds");
		//todo solve exception
		int[] userIds = new int[ids.length];
		for(int i = 0; i < ids.length; i++) {
			userIds[i] = Integer.parseInt(ids[i]);
		}
		
		StudentService service=new StudentService();
		JSONObject json=service.deleteStudents(userIds);
		response.getWriter().write(json.toString());
	}
	/**
	 * 删除学生信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void modifyStudent(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String stuname = request.getParameter("stuname");
		String stuclass = request.getParameter("stuclass");
		String major = request.getParameter("major");
		String stunum = request.getParameter("stunum");
		String str=request.getParameter("user_id");
		//todo solve exception
		int user_id = Integer.parseInt(str);
		
		Student stu = new Student();
		stu.setMajor(major);
		stu.setStuclass(stuclass);
		stu.setStunum(stunum);
		stu.setStuname(stuname);
		stu.setUser_id(user_id);
		
		StudentService service=new StudentService();
		JSONObject json=service.modify(stu);
		response.getWriter().write(json.toString());
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
